package com.crmapi.controller;

import com.crmapi.group.Update;
import com.crmapi.pojo.PageBean;
import com.crmapi.pojo.Product;
import com.crmapi.pojo.Result;
import com.crmapi.service.ProductService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

@Slf4j
@Api(tags = "产品管理")
@RestController
@RequestMapping("/api_v1.0")
public class ProductController extends BaseController {

    @Autowired
    ProductService productService;

    @ApiOperation("保存产品")
    @PostMapping("product/save")
    public Result save(@Validated @RequestBody Product product, BindingResult result){
        validator(result);
        int rs = productService.save(product);
        return Result.success(rs);
    }

    @ApiOperation("删除产品")
    @DeleteMapping("product/delete/{ids}")
    public Result delete(@PathVariable List<String> ids){
        log.info("批量删除,ids:{}",ids);
        int rs = productService.delete(ids);
        if(rs == 0)
            return Result.error("删除失败");
        return Result.success("删除成功");
    }

    @ApiOperation("更新产品")
    @PutMapping("product/update")
    public Result update(@Validated(Update.class)@RequestBody Product product,BindingResult result){
        validator(result);
        int rs = productService.update(product);
        if(rs==0)
            return Result.error("更新失败");
        return Result.success("更新成功");
    }

    @ApiOperation("分页条件查询")
    @PostMapping("/product/list")
    public Result queryList(@RequestParam(defaultValue = "1") Integer page,
                            @RequestParam(defaultValue = "10") Integer pageSize,
                            String code, String name, Double price, Integer num, Integer status, String note){
        log.info("分页条件参数{},{},{},{},{},{},{},{}",page,pageSize,code,name,price,num,status,note);
        PageBean pageBean = productService.queryList(page,pageSize,code,name,price,num,status,note);
        return Result.success(pageBean);
    }

    @ApiOperation("查询产品")
    @GetMapping ("/product/all")
    public Result query(){
        List<Product> productList= productService.queryAll();
        return Result.success(productList);
    }

}
